package com.manor.main.base;

import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import androidx.core.content.ContextCompat;
import android.view.Window;

import com.manor.common.utils.ActivityManager;
import com.manor.main.fragment.community.Utils;

import static android.view.FrameMetrics.ANIMATION_DURATION;

/**
 * created by Qin
 * on 2020/3/28
 */
public class AnimatorManager {


    private static AnimatorManager instance;
    private Window window;
    private int statusBarColor;
    private static final String STATUS_BAR_COLOR = "statusBarColor";
    private Animator actionModeInAnimator, actionModeOutAnimator;

    private AnimatorManager() {

    }

    public static AnimatorManager getInstance() {
        if (instance == null) {
            instance = new AnimatorManager();
        }
        return instance;
    }

    public void initAnimator(Activity mActivity, int color) {
        window = mActivity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            statusBarColor = window.getStatusBarColor();
            int actionModeStatusBarColor = ContextCompat.getColor(mActivity, color);
            int startColor = Color.argb(
                    0,
                    Color.red(actionModeStatusBarColor),
                    Color.green(actionModeStatusBarColor),
                    Color.blue(actionModeStatusBarColor)
            );
            actionModeInAnimator = ObjectAnimator.ofObject(
                    window,
                    STATUS_BAR_COLOR,
                    new ArgbEvaluator(),
                    startColor,
                    actionModeStatusBarColor
            );
            actionModeOutAnimator = ObjectAnimator.ofObject(
                    window,
                    STATUS_BAR_COLOR,
                    new ArgbEvaluator(),
                    actionModeStatusBarColor,
                    startColor
            );
            actionModeInAnimator.setDuration(ANIMATION_DURATION);
            actionModeOutAnimator.setDuration(ANIMATION_DURATION);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            actionModeOutAnimator.cancel();
            actionModeInAnimator.setDuration(ANIMATION_DURATION).start();
            Utils.setLightStatusBar(window.getDecorView());
        }
    }

    public void closeAnimator(Activity mActivity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setStatusBarColor(statusBarColor);
            if (actionModeInAnimator != null) {
                actionModeInAnimator.cancel();
            }
            if (actionModeOutAnimator != null) {
                actionModeOutAnimator.start();
            }
            Utils.clearLightStatusBar(window.getDecorView());
        }
        ActivityManager.getInstance().removeActivity(mActivity);
    }

}
